//
//  PageTitleView.swift
//  StudyDY
//
//  Created by yinchangming on 2018/6/29.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit

protocol PageTitleViewProtocol : class{
    /***
     
     titleView 选择的View
     currentPageIndex 选中标题的角标
     **/
    @available(iOS 11.0, *)
    func selecedPage(titleView:PageTitleView,currentPageIndex:Int)
}



/**显示标题的View**/
@available(iOS 11.0, *)
class PageTitleView: UIView {
    private var title:[String]
        //懒加载 显示标题的sc
    @available(iOS 11.0, *)
    lazy var uiSrcooView = { ()->UIScrollView in
        let sc=UIScrollView()
        sc.showsVerticalScrollIndicator=false
        sc.scrollsToTop=false
        sc.bounces=false
        sc.frame=CGRect(x: 0, y: 0, width: kScreenW, height: 40)
        sc.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentBehavior.never
        return sc
    }()
    //懒加载 标题下面的 橙色线
    lazy var orangeLine={ ()->UIView in
        let orangeL=UIView()
        orangeL.backgroundColor=UIColor.orange
        return orangeL
    }()
    //懒加载 装lable的数组
    lazy var titleLabels = [UILabel]()
    //被点击的lable角标
    var clickLableIndex=0
    weak var  delegte:PageTitleViewProtocol?
    init(frame:CGRect,titles:[String]){
        self.title=titles
        super.init(frame: frame)
         updateUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
@available(iOS 11.0, *)
extension PageTitleView{
    private func updateUI(){
        //添加滚动控件
        addSubview(uiSrcooView)
        //2 给sc添加控件
         setScLable()
        //3 添加sc底部线
        setScBottomLine()
    }
    /**添加sc ***/
    private func setScLable(){
        let lableW=frame.width / CGFloat(title.count)
        let lableH=frame.height
        let lableY=0
        for (index,tilteName) in title.enumerated(){
            let leble=UILabel()
            leble.text=tilteName
            leble.tag=index
            leble.textColor=UIColor.darkGray
            leble.textAlignment = .center//字的位置
            let lableX=CGFloat(index)  * lableW
    
            leble.frame=CGRect(x: lableX, y:CGFloat(lableY), width: lableW, height: lableH)
            uiSrcooView.addSubview(leble)
            titleLabels.insert(leble, at: index)
            
            //给lable 添加手势
            leble.isUserInteractionEnabled=true
            let lableGes=UITapGestureRecognizer(target: self, action: #selector(lableClick(ges:)))
            leble.addGestureRecognizer(lableGes)
        }
    }
    func setScBottomLine(){
        //添加底部灰色线
        let bottomLine=UIView()
        bottomLine.backgroundColor=UIColor.lightGray
        let bottomLineH:CGFloat=0.5
        bottomLine.frame =  CGRect(x: 0, y: frame.height-bottomLineH, width: kScreenW, height: bottomLineH)
        addSubview(bottomLine)
        
        //添加选择标题 下面线与字体颜色
        guard let firstLable =   titleLabels.first else {return}
        firstLable.textColor=UIColor.orange
        orangeLine.frame=CGRect(x: firstLable.frame.origin.x, y: frame.height-CGFloat(2), width: firstLable.frame.width, height: CGFloat(2))
        uiSrcooView.addSubview(orangeLine)
    }
}
//lable的点击事件
@available(iOS 11.0, *)
extension PageTitleView{
    @objc private func lableClick(ges:UITapGestureRecognizer){
        //1 获取被点击的lable
        let currentLab=ges.view as! UILabel
        //反复点击一个lebel 直接返回不执行下面内容
        if(currentLab.tag==clickLableIndex){return}
        // 2获取前一个被点击Lab
        let oldClickedLab=titleLabels[clickLableIndex]
        // 3 更改老label 与新点击label 颜色
        
         resetLabelColor()
        
        currentLab.textColor=UIColor.orange
        oldClickedLab.textColor=UIColor.darkGray
        clickLableIndex=currentLab.tag

        // 4 更改当前label下面线的位置(更改 orangeLine 的x的值就ok)
        let currentLabX=CGFloat(clickLableIndex)  * currentLab.frame.width
        UIView.animate(withDuration: 0.5) {
            self.orangeLine.frame.origin.x=currentLabX
        }
        delegte?.selecedPage(titleView: self, currentPageIndex: clickLableIndex)
    }
    func resetLabelColor(){
        for lable in titleLabels{
                lable.textColor=UIColor.darkGray
        }
    }
}
//暴露出给其他控制器的方法
@available(iOS 11.0, *)
extension PageTitleView{
    func setCurrentLabelWithProgress(progress:CGFloat,currentPage:Int,mubiaoPage:Int){
//        print("进度:\(progress)    当前页:\(currentPage)  目标页:\(mubiaoPage)   ")
      // 如果 目标页大于总页数 或者小于0 返回不做操作
        if(mubiaoPage>title.count || mubiaoPage<0){return}//||((currentPage==mubiaoPage) && currentPage != 0)
        let currentLable=titleLabels[currentPage]
        let mubiaoLable=titleLabels[mubiaoPage]
        //第一页 与最后一月从新刷新一下字体颜色
        if(mubiaoPage==0||mubiaoPage==titleLabels.count-1){
             resetLabelColor()
        }
        //当 进度大于0.5的时候 更换字体颜色 并更改下划线
        if(progress >= 0.5){
           
            let  offsetX = currentLable.frame.width * CGFloat( mubiaoPage)
            UIView.animate(withDuration: 1) {
           self.orangeLine.frame=CGRect(x:offsetX, y: self.frame.height-CGFloat(2), width: currentLable.frame.width, height: CGFloat(2))
           currentLable.textColor=UIColor.darkGray
           mubiaoLable.textColor=UIColor.orange
            }
        }
 
    }
}
